package com.example.bbs.service.impl;

import com.example.bbs.dao.UserDao;
import com.example.bbs.dao.impl.UserDaoImpl;
import com.example.bbs.model.User;
import com.example.bbs.service.UserService;
import com.example.bbs.utils.TimeUtil;

import java.text.ParseException;
import java.util.Date;

/**
 * @auther: zhangxin
 * @Description:
 */
public class UserServiceImpl implements UserService {


    UserDao userDao = new UserDaoImpl();

    @Override
    public boolean doReg(User user) {
        Date date = null;
        try {
            date = TimeUtil.getCurrentTimeToDate();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setCreateTime(date);
        user.setPostNumber(0);
        int ra = (int) Math.floor(Math.random() * 5 + 1);
        user.setUserHead("imgs/head0" + ra + ".jpg");
        user.setLoginTime(date);
        return userDao.doReg(user);
    }

    @Override
    public User doLogin(User user) {

        user = userDao.findUserByNameAndPwd(user);
        return user;
    }

    @Override
    public User findUserById(int userId) {

        return userDao.findUserById(userId);
    }

    @Override
    public User findUserByPostId(int postId) {
        return userDao.findUserByPostId(postId);
    }

    @Override
    public boolean updatePwd(User user, String newPwd) {
        return userDao.updatePwd(user,newPwd);
    }

    @Override
    public boolean updateLoginTime(int userId) {
        Date loginTime = null;
        try {
            loginTime = TimeUtil.getCurrentTimeToDate();
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return userDao.updateLoginTime(userId,loginTime);
    }

    @Override
    public User findUserByUser(User user) {
        return userDao.findUserByNameAndPwd(user);
    }

    @Override
    public User findUserByName(User user) {
        return userDao.findUserByName(user);
    }
}
